package com.dagene.weixin.util;

import java.io.UnsupportedEncodingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dagene.weixin.bean.AccessToken;
import com.dagene.weixin.bean.Button;
import com.dagene.weixin.bean.CommonButton;
import com.dagene.weixin.bean.ComplexButton;
import com.dagene.weixin.bean.Menu;
import com.dagene.weixin.bean.ViewButton;
import com.dagene.weixin.service.CoreService;

/**
 * 菜单管理器类
 * 
 * @author liufeng
 * @date 2013-08-08
 */
public class MenuManager {
	private static Logger log = LoggerFactory.getLogger(MenuManager.class);

	static final String APPID = Constants.APPID; // "wx68890c5cebee1c8e";
	static final String APPSECRET = Constants.APPSECRET; // "48a3424c91cc9ee78fa72505bc5f1cb7";

	public static void main(String[] args) throws UnsupportedEncodingException {
		HttpUtil httpUtil = new HttpUtil();
		try {
			// 调用接口获取access_token

			/*
			 * JSONObject jsonobj =
			 * MediaUtil.getUserInfo("o5QX1jnh2et-Iv6N9j_utEoxY8vw");
			 * 
			 * String nickname = jsonobj.getString("nickname");
			 * 
			 * System.out.println(nickname);
			 */
			AccessToken at = httpUtil.getAccessToken(APPID, APPSECRET);

			// AccessToken at = httpUtil.getAccessToken(peisongAPPID,
			// peisongAPPSECRET);

			httpUtil.httpGetRequest(
					"https://api.weixin.qq.com/cgi-bin/menu/delete?access_token="
							+ at.getToken(), null);

			if (null != at) {
				// 调用接口创建菜单
				int result = httpUtil.createMenu(getMenu(), at.getToken());
				System.out.println(result);
				// 判断菜单创建结果
				// if (0 == result)
				// log.info("菜单创建成功！");
				// else
				// log.info("菜单创建失败，错误码：" + result);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * 组装菜单数据,迪安人
	 * 
	 * @return
	 */
	private static Menu getMenu() {

		ViewButton btn1_2 = new ViewButton();
		btn1_2.setName("新闻资讯");
		btn1_2.setType("view");
		btn1_2.setUrl(Constants.weixinurl + "/phoneu/index.html");

		ViewButton btn1_3 = new ViewButton();
		btn1_3.setName("掌上商城");
		btn1_3.setType("view");
		btn1_3.setUrl(Constants.weixinurl + "/phoneu/shoplist.html");

		/*ComplexButton mainBtn1 = new ComplexButton();
		mainBtn1.setName("快捷报修");
		mainBtn1.setSub_button(new Button[] { btn1_2, btn1_3 });

		ViewButton btn2_1 = new ViewButton();
		btn2_1.setName("专题分享");
		btn2_1.setType("view");
		btn2_1.setUrl(Constants.weixinurl + "/weixiu/topicshare.html");

		ViewButton btn2_2 = new ViewButton();
		btn2_2.setName("故障排查");
		btn2_2.setType("view");
		btn2_2.setUrl(Constants.weixinurl + "/weixiu/troublefind.html");

		ViewButton btn2_3 = new ViewButton();
		btn2_3.setName("操作指南");
		btn2_3.setType("view");
		btn2_3.setUrl(Constants.weixinurl + "/weixiu/operationguide.html");

		ComplexButton mainBtn2 = new ComplexButton();
		mainBtn2.setName("自助服务");
		mainBtn2.setSub_button(new Button[] { btn2_1, btn2_2, btn2_3 });*/

		CommonButton btn3_1 = new CommonButton();
		btn3_1.setName("客服咨询");
		btn3_1.setType("click");
		btn3_1.setKey("400");
		
		ViewButton btn3_2 = new ViewButton();
		btn3_2.setName("虚拟社区");
		btn3_2.setType("view");
		btn3_2.setUrl(Constants.weixinurl + "/phoneu/topicmenu.html");
		
		ComplexButton mainBtn3 = new ComplexButton();
		mainBtn3.setName("交流中心");
		mainBtn3.setSub_button(new Button[] { btn3_1, btn3_2 });

		Menu menu = new Menu();

		menu.setButton(new Button[] { btn1_2, btn1_3, mainBtn3 });

		return menu;
	}
}
